 /*
 * ArCoLIVE - Live Internet Videoconference Environment
 * Information Technology Departament - Federal University of Alagoas - Brazil
 *
 * Copyright (C) 2005 Leandro Melo de Sales (leandro@labpesquisas.tci.ufal.br)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.ufal.tci.nexos.arcolive.protocol;

import java.util.ArrayList;

import br.ufal.tci.nexos.arcolive.ArCoLIVEServerProperties;
import br.ufal.tci.nexos.arcolive.command.ArCoLIVECommandResult;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotProcessCommandException;
import br.ufal.tci.nexos.arcolive.service.connection.ConnectionRequest;

/**
 * CLASSNAME.java
 *
 * CLASS DESCRIPTION
 *
 * @see CLASSNAME
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>yyyymmdd USERNAME:</b>
 * <ul>
 * <li> VERSION
 * </ul>
 */
public abstract class ArCoLIVEProtocol {

	// ArrayList de ArCoLIVE commands
	private ArrayList commands;
	private int serviceId;
	
	public ArCoLIVEProtocol(int serviceId) {
		if (ArCoLIVEServerProperties.getInstance().getStringProperty("server.commandHistory").equalsIgnoreCase("yes")) {
			this.commands = new ArrayList();
		}
		this.serviceId = serviceId;
	}	

	/*public ArCoLIVEProtocol(int serviceId, int serviceInstanceId) {
		if (ServerProperties.getInstance().getStringProperty("server.commandHistory").equalsIgnoreCase("yes")) {
			this.commands = new ArrayList();
		}
		this.serviceId = serviceId;
		this.serviceInstanceId = serviceInstanceId;
	}*/
	
	public ArCoLIVECommandResult handleRequest(ConnectionRequest request) throws ArCoLIVECannotProcessCommandException {
		ArCoLIVECommandResult commandResult = this.handleRequestImpl(request);
		if (commandResult == null) {
			throw new ArCoLIVECannotProcessCommandException("Error while processing client command");
		}
		if (ArCoLIVEServerProperties.getInstance().getStringProperty("server.commandHistory").equalsIgnoreCase("yes")) {
			this.commands.add(commandResult);
		}
		return commandResult;
	}
	
	public String encodeMessage(int commandId) {
		return encodeMessageImpl(commandId);
	}
	
	public boolean equals(Object object) {
		if (object instanceof ArCoLIVEProtocol) {
			ArCoLIVEProtocol protocol = (ArCoLIVEProtocol)object;
			return (this.hashCode() == protocol.hashCode());
		} else {
			return false;
		}
	}
	
	public int hashCode() {
		return this.serviceId;
	}
	
	protected abstract ArCoLIVECommandResult handleRequestImpl(ConnectionRequest request) throws ArCoLIVECannotProcessCommandException;
	protected abstract String encodeMessageImpl(int commandId);
	
//	public static void main(String[] args) {
//		String command1 = "200#param1=value1#a=a";
//		if (ArCoLIVEProtocol.parseCommand(command1)) {
//			ArCoLIVELogRepository.getInstance().log("Validou!");
//		} else {
//			ArCoLIVELogRepository.getInstance().log("N?o validou!");
//		}
//	}
	
}
